#import "ActivityDetailUI.h"
#import "UIView+Toast.h"
#import "NetManager.h"
#import "ASIFormDataRequest.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
@interface ActivityDetailUI ()

@end

@implementation ActivityDetailUI
@synthesize activityId;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle=NO;
    self.photos=[[NSMutableArray alloc] initWithCapacity:8];
    self.thumbs = [[NSMutableArray alloc] init];
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    
    UIButton* cancelBtn= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [cancelBtn addTarget:self action:@selector(backFn) forControlEvents:UIControlEventTouchUpInside];
    CGRect frame_1= CGRectMake(0,12, 20,20);
    UIImageView*  backView=[[UIImageView alloc] initWithFrame:frame_1];
    UIImage* image= [UIImage imageNamed:@"back2016"];
    backView.image=image;
    [cancelBtn addSubview:backView];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    
    
    UILabel* titleLb=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
    titleLb.text=@"活动详情";
    titleLb.font=[UIFont systemFontOfSize:HEAD_SIZE];
    titleLb.backgroundColor=[UIColor clearColor];
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=titleLb;
    
    UIButton*  jubaoBtn=[[UIButton alloc] initWithFrame:CGRectMake(240, 5, 30, 34)];
    [jubaoBtn addTarget:self action:@selector(jubaoFn) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel* jubaoLb=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 34)];
    jubaoLb.text=@"举报";
    jubaoLb.font=[UIFont systemFontOfSize:14];
    jubaoLb.textColor=[UIColor whiteColor];
    jubaoLb.backgroundColor=[UIColor clearColor];
    [jubaoBtn addSubview:jubaoLb];
    
    
    
    [NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:jubaoBtn];
    
}
-(void)jubaoFn{
    [self.view makeToast:@"举报成功,我们的工作人员会尽快处理您反馈的信息" duration:1.0 position:@"center"];
}


-(void)loadData{
    
    
    NSString* loginURL=HOST;
    loginURL=[loginURL stringByAppendingString:@"/index.php/AppActivity/loadDetail"];
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    
    
    [req addPostValue:[NSString stringWithFormat:@"%d",self.activityId] forKey:@"act_id"];
    
    [req startSynchronous];
    NSError *error = [req error];
    if (!error) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [MBProgressHUD hideHUDForView:app.window animated:YES];
        NSString *response = [req responseString];
        NSLog(@"返回结果..%@",response);
        
        
        NSLog(@"Request Successful, response '%@'", response);
        dic=[response JSONValue];
        
        //
        UILabel*  themePreLb=[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 40, 20)];
        themePreLb.text=@"主题";
        themePreLb.font=[UIFont boldSystemFontOfSize:15];
        [self.view addSubview:themePreLb];
        //
        UILabel*  themeLb=[[UILabel alloc] initWithFrame:CGRectMake(80, 20, 240, 20)];
        themeLb.text=dic[@"name"];
        themeLb.textAlignment=NSTextAlignmentLeft;
        themeLb.font=[UIFont systemFontOfSize:15];
        [self.view addSubview:themeLb];
        
        
        
        
        ////
        UIImageView* aoxianView=[[UIImageView alloc] initWithFrame:CGRectMake(3, 60, 320-6, 2.5)];
        aoxianView.image=[UIImage imageNamed:@"aoxian.png"];
        [self.view addSubview:aoxianView];
        
        ////////
        UILabel*  activity_timePreLb=[[UILabel alloc] initWithFrame:CGRectMake(20, 20+60, 40, 20)];
        activity_timePreLb.text=@"时间";
        activity_timePreLb.font=[UIFont boldSystemFontOfSize:15];
        [self.view addSubview:activity_timePreLb];
        //
        UILabel*  activity_timeLb=[[UILabel alloc] initWithFrame:CGRectMake(80, 20+60, 240, 20)];
        activity_timeLb.text=dic[@"activity_time"];
        activity_timeLb.textAlignment=NSTextAlignmentLeft;
        activity_timeLb.font=[UIFont systemFontOfSize:15];
        [self.view addSubview:activity_timeLb];
        
        //
        aoxianView=[[UIImageView alloc] initWithFrame:CGRectMake(3, 125, 320-6, 2.5)];
        aoxianView.image=[UIImage imageNamed:@"aoxian.png"];
        [self.view addSubview:aoxianView];
        
        
        //////////
        
        
        UIImageView*  addrPreView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 143, 20, 20)];
        addrPreView.image=[UIImage imageNamed:@"m_addr"];
        [self.view addSubview:addrPreView];
        
        
        UILabel*  addrLb=[[UILabel alloc] initWithFrame:CGRectMake(80, 143, 240, 20)];
        addrLb.text=dic[@"address"];
        addrLb.textAlignment=NSTextAlignmentLeft;
        addrLb.font=[UIFont systemFontOfSize:15];
        [self.view addSubview:addrLb];
        
        //
        aoxianView=[[UIImageView alloc] initWithFrame:CGRectMake(3, 182, 320-6, 2.5)];
        aoxianView.image=[UIImage imageNamed:@"aoxian.png"];
        [self.view addSubview:aoxianView];
        //
        
        UILabel*  descLb=[[UILabel alloc] initWithFrame:CGRectMake(20, 193, 240, 20)];
        descLb.text=@"详情";
        descLb.textAlignment=NSTextAlignmentLeft;
        descLb.font=[UIFont systemFontOfSize:15];
        [self.view addSubview:descLb];
        
        /////////////
        int n=4;
        UIScrollView* sv=[[UIScrollView alloc] initWithFrame:CGRectMake(0,210, 320, 100)];
        
        sv.accessibilityActivationPoint = CGPointMake(110*n, 100);
        sv.contentSize = CGSizeMake(400,
                                    0);
        sv.showsHorizontalScrollIndicator = FALSE;
        sv.delegate = self;
        [self.view addSubview:sv];
        
        
        
        
        UIImageView* view=nil;
        NSString* pic0=[dic objectForKey:@"pic0"];
        NSString* pic1=[dic objectForKey:@"pic1"];
        NSString* pic2=[dic objectForKey:@"pic2"];
        NSString* pic3=[dic objectForKey:@"pic3"];
        NSLog(@"图片地址==pic0=====%@",pic0);
        
        MWPhoto* mwPhoto=nil;
        
        
        if (pic0!=nil&&pic0.length>0) {
            //pic0=[@"ios/" stringByAppendingString:pic0];
            pic0=[HOST stringByAppendingString:pic0];
            NSLog(@"图片地址==pic0=====%@",pic0);
            view=[[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 100*0.71, 140*0.71)];
            [view setImageWithURL:[NSURL URLWithString:pic0]];
            view.tag=0;
            
            view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
            [view addGestureRecognizer:singleTap];
            
            mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic0]];
            
            [self.photos addObject:mwPhoto];
            [self.thumbs addObject:mwPhoto];
            
            [sv addSubview:view];
            
        }
        if (pic1!=nil&&pic1.length>0) {
            //pic1=[@"ios/" stringByAppendingString:pic1];
            pic1=[HOST stringByAppendingString:pic1];
            NSLog(@"图片地址==pic1=====%@",pic1);
            view=[[UIImageView alloc] initWithFrame:CGRectMake(10+100*0.71+10, 0, 100*0.71, 140*0.71)];
            [view setImageWithURL:[NSURL URLWithString:pic1]];
            view.tag=1;
            
            view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
            [view addGestureRecognizer:singleTap];
            
            mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic1]];
            
            [self.photos addObject:mwPhoto];
            [self.thumbs addObject:mwPhoto];
            
            [sv addSubview:view];
            
        }
        if (pic2!=nil&&pic2.length>0) {
            //pic2=[@"ios/" stringByAppendingString:pic2];
            pic2=[HOST stringByAppendingString:pic2];
            NSLog(@"图片地址==pic1=====%@",pic2);
            view=[[UIImageView alloc] initWithFrame:CGRectMake(30+100*0.71*2, 0, 100*0.71, 140*0.71)];
            [view setImageWithURL:[NSURL URLWithString:pic2]];
            view.tag=2;
            
            view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
            [view addGestureRecognizer:singleTap];
            
            mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic2]];
            
            [self.photos addObject:mwPhoto];
            [self.thumbs addObject:mwPhoto];
            
            [sv addSubview:view];
            
        }
        if (pic3!=nil&&pic3.length>0) {
            //pic3=[@"ios/" stringByAppendingString:pic3];
            pic3=[HOST stringByAppendingString:pic3];
            NSLog(@"图片地址==pic3=====%@",pic3);
            view=[[UIImageView alloc] initWithFrame:CGRectMake(40+100*0.71*3, 0, 100*0.71, 140*0.71)];
            [view setImageWithURL:[NSURL URLWithString:pic3]];
            view.tag=3;
            
            view.userInteractionEnabled=YES;//这句话必须加 否则无法点击
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
            [view addGestureRecognizer:singleTap];
            
            mwPhoto=[MWPhoto photoWithURL:[NSURL URLWithString:pic3]];
            
            [self.photos addObject:mwPhoto];
            [self.thumbs addObject:mwPhoto];
            
            [sv addSubview:view];
            
        }
        
        
        
    }
    
    
}
#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
    if (index < _thumbs.count)
        return [_thumbs objectAtIndex:index];
    return nil;
}
-(void)yourHandlingCode:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"点击图片");
    
    UIImageView *view = (UIImageView*)[gestureRecognizer view];
    int tag = view.tag;
    
    
    BOOL displayActionButton = YES;
    BOOL displaySelectionButtons = NO;
    BOOL displayNavArrows = NO;
    BOOL enableGrid = YES;
    BOOL startOnGrid = NO;
    
    
    //Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    
    browser.displayActionButton = displayActionButton;
    browser.displayNavArrows = displayNavArrows;
    browser.displaySelectionButtons = displaySelectionButtons;
    browser.alwaysShowControls = displaySelectionButtons;
    browser.zoomPhotosToFill = YES;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;
#endif
    browser.enableGrid = enableGrid;
    browser.startOnGrid = startOnGrid;
    browser.enableSwipeToDismiss = YES;
    //    browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    //    browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    //    browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    //    browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    //    browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    //    browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    //    browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    //    browser.wantsFullScreenLayout = YES; // iOS 5 & 6 only: Decide if you want the photo browser full screen, i.e. whether the status bar is affected (defaults to YES)
    
    
    
    
    [browser setCurrentPhotoIndex:tag];
    
    // Reset selections
    //    if (displaySelectionButtons) {
    //        _selections = [NSMutableArray new];
    //        for (int i = 0; i < photos.count; i++) {
    //            [_selections addObject:[NSNumber numberWithBool:NO]];
    //        }
    //    }
    
    // Show
    
    [self.navigationController pushViewController:browser animated:YES];
    
    // Release
    
    // Deselect
    //[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // Test reloading of data after delay
    double delayInSeconds = 3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        
    });
    
    
}

-(void)backFn{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
